import {Global} from "../Global";

export class HttpHelper{
    
    /**
     * 提交表单
     * @param url 根目录路径 如：/Doc
     * @param bodyObj 参数对象
     * @returns 服务器返回信息
     */
    static post = async (url: string, bodyObj: any): Promise<string> => {

        var bodyStr: string = "";

        if (bodyObj) {
            for (var pro in bodyObj) {
                var val = bodyObj[pro];
                if (val == undefined || val == null)
                    continue;
                if (bodyStr.length > 0)
                    bodyStr += "&";

                if (Array.isArray(val)) {
                    for (var i = 0; i < val.length; i++) {
                        if (i > 0)
                            bodyStr += "&";
                        bodyStr += pro + "=" + encodeURIComponent(val[i]);
                    }
                }
                else {
                    bodyStr += pro + "=" + encodeURIComponent(val);
                }
            }
        }

        var ret = await fetch(`${Global.ServerUrl}${url}`, {
            method: 'POST',
            headers: {
                'Content-Type': "application/x-www-form-urlencoded",
                'Authorization': Global.UserInfo.Token
            },
            body: bodyStr
        });
        var text = await ret.text();
        if (ret.status >= 300 || ret.status < 200) {
            if (text)
                throw { statusCode: ret.status , msg:text};
            else
                throw { statusCode: ret.status };
        }

        return text;


    };

     /**
     * 提交表单
     * @param url 根目录路径 如：/Doc
     * @param bodyObj 参数对象
     * @returns 服务器返回信息
     */
     static postForBlob = async (url: string, bodyObj: any): Promise<Blob> => {

        var bodyStr: string = "";

        if (bodyObj) {
            for (var pro in bodyObj) {
                var val = bodyObj[pro];
                if (val == undefined || val == null)
                    continue;
                if (bodyStr.length > 0)
                    bodyStr += "&";

                if (Array.isArray(val)) {
                    for (var i = 0; i < val.length; i++) {
                        if (i > 0)
                            bodyStr += "&";
                        bodyStr += pro + "=" + encodeURIComponent(val[i]);
                    }
                }
                else {
                    bodyStr += pro + "=" + encodeURIComponent(val);
                }
            }
        }

        var ret = await fetch(`${Global.ServerUrl}${url}`, {
            method: 'POST',
            headers: {
                'Content-Type': "application/x-www-form-urlencoded",
                'Authorization': Global.UserInfo.Token
            },
            body: bodyStr
        });

        if (ret.status >= 300 || ret.status < 200) {
            var text = await ret.text();
            if (text)
                throw { statusCode: ret.status , msg:text};
            else
                throw { statusCode: ret.status };
        }

        return await ret.blob();


    };

    static postJson = async (url: string, bodyObj: any): Promise<string> => {


        var ret = await fetch(`${Global.ServerUrl}${url}`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Authorization': Global.UserInfo.Token
            },
            body: JSON.stringify(bodyObj)
        });

        var text = await ret.text();
        if (ret.status >= 300 || ret.status < 200) {
            if (text)
                throw { statusCode: ret.status , msg:text};
            else
                throw { statusCode: ret.status };
        }

        return text;


    };

    static postForm = async (url: string, jsonObj : any): Promise<string> => {
        var body = new FormData();
        for(var p in jsonObj){
            body.append(p , jsonObj[p]);
        }
        var ret = await HttpHelper.postFormData(url , body);
        return ret;
    };

    static postFormData = async (url: string, formdata: FormData): Promise<string> => {


        var ret = await fetch(`${Global.ServerUrl}${url}`, {
            method: 'POST',
            headers: {
                'Authorization': Global.UserInfo.Token
            },
            body: formdata
        });
        var text = await ret.text();
        if (ret.status >= 300 || ret.status < 200) {
            if (text)
                throw { statusCode: ret.status , msg:text};
            else
                throw { statusCode: ret.status };
        }

        return text;


    };

    /**
     * 
     * @param url 
     * @param bodyObj 
     * @returns 
     */
    static get = async (url: string, bodyObj: any, token: any = null): Promise<any> => {

        var bodyStr: string = "";

        if (bodyObj) {
            for (var pro in bodyObj) {
                var val = bodyObj[pro];
                if (val == undefined || val == null)
                    continue;
                if (bodyStr.length > 0)
                    bodyStr += "&";

                if (Array.isArray(val)) {
                    for (var i = 0; i < val.length; i++) {
                        if (i > 0)
                            bodyStr += "&";
                        bodyStr += pro + "=" + encodeURIComponent(val[i]);
                    }
                }
                else {
                    bodyStr += pro + "=" + encodeURIComponent(val);
                }
            }
        }

        if (url.indexOf("?") > 0)
            url += "&" + bodyStr;
        else
            url += "?" + bodyStr;

        var ret = await fetch(`${Global.ServerUrl}${url}`, {
            method: 'GET',
            headers: {
                'Authorization': token ? token : Global.UserInfo.Token
            }
        });

        var text = await ret.text();
        if (ret.status >= 300 || ret.status < 200) {
            if (text)
                throw { statusCode: ret.status , msg:text};
            else
                throw { statusCode: ret.status };
        }

        return text;

    };

     /**
     * 
     * @param url 
     * @param bodyObj 
     * @returns 
     */
     static getForBlob = async (url: string, bodyObj: any, token: any = null): Promise<Blob> => {

        var bodyStr: string = "";

        if (bodyObj) {
            for (var pro in bodyObj) {
                var val = bodyObj[pro];
                if (val == undefined || val == null)
                    continue;
                if (bodyStr.length > 0)
                    bodyStr += "&";

                if (Array.isArray(val)) {
                    for (var i = 0; i < val.length; i++) {
                        if (i > 0)
                            bodyStr += "&";
                        bodyStr += pro + "=" + encodeURIComponent(val[i]);
                    }
                }
                else {
                    bodyStr += pro + "=" + encodeURIComponent(val);
                }
            }
        }

        if (url.indexOf("?") > 0)
            url += "&" + bodyStr;
        else
            url += "?" + bodyStr;

        var ret = await fetch(`${Global.ServerUrl}${url}`, {
            method: 'GET',
            headers: {
                'Authorization': token ? token : Global.UserInfo.Token
            }
        });

       
        if (ret.status >= 300 || ret.status < 200) {
            var text = await ret.text();
            if (text)
                throw { statusCode: ret.status , msg:text};
            else
                throw { statusCode: ret.status };
        }

        return await ret.blob();

    };
}